package com.itheima.demo02;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class ReceiveMassageDemo02 {
    //利用 UPD 接收数据

    public static void main(String[] args) throws IOException {
       //创建 DatagramSocket 对象
        DatagramSocket ds = new DatagramSocket(10087);

       while (true){
           //打包数据
           byte[] bytes = new byte[1024];

           DatagramPacket dp=new DatagramPacket(bytes,0,bytes.length);

           //接收数据
           ds.receive(dp);

           //解析并打印数据
           String hostAddress = dp.getAddress().getHostAddress();
           int length = dp.getLength();
           byte[] data = dp.getData();

           System.out.println(hostAddress + "向你发送消息:" + new String(data,0,length));
       }

        //释放资源
//        ds.close();


    }

}
